!localStorage.getItem("nanoid") && localStorage.setItem("nanoid", nanoid(16));
let controller = new AbortController();
var fetchHomeData = async () => {
  controller.abort();
  controller = new AbortController();
  var domainList = [
    "o-jp-feeds.mobicrazy.com",
    "o-mx-feeds.mobicrazy.com",
    "o-co-feeds.mobicrazy.com",
    "o-ar-feeds.mobicrazy.com",
    "o-pt-rbr-feeds.mobicrazy.com",
  ];
  let url = buildUrl(
    domainList.includes(document.domain)
      ? "/MediaMotech/index.html"
      : "/MediaOp/index.html",
    {
      publisherId: "Vilykke",
      key: "c9332c3480a2abaa8ee2b2be8d1f4c58",
      language: getSearchParam("language") ?? "en",
      countryCode: getSearchParam("countryCode") ?? "IN",
      imei: getSearchParam("imei") ?? localStorage.getItem("nanoid"),
      category: "Hot",
      limit: 10,
      test: getSearchParam("test"),
      mbtype: domainList.includes(document.domain) ? "promoted" : "",
    }
  );

  return http({
    method: "GET",
    url,
    responseType: "json",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    signal: controller.signal,
  });
};
var sendTrack = (url) => fetch(url);
var handleClick = (event, d_key) => {
  var item = dataList.find((item) => item.d_key + "" === d_key + "");
  document.querySelectorAll(`._${d_key}`).forEach((el) => {
    el.classList.add("active");
  });
  window.location.href = item.url;
};

function insertAdsHTML(adsID, parent, target, elIndex) {
  var hostname = window.location.hostname;
  var index = target.getAttribute("index");
  var parser = new DOMParser();
  var defaultID = window.plainAdsSlotID["default"];
  // window.domains.includes(hostname) &&
  //   loadScript03((adsID ? adsID : defaultID).replace("-0", `-${index}`));
  var frament =
    window.location.hostname === "hi-feeds.obnews.co"
      ? `
  <div class='plain-ads'>
    <div class='plain-ads-child' id='${(adsID ? adsID : defaultID).replace(
      "-0",
      `-${index}`
    )}' style='width: 300px; min-height: 250px;'>
      <script class='script-copy'>
        window.googletag = window.googletag || {cmd: []};
          googletag.cmd.push(function() {
            googletag.defineSlot('/23045591466/hi-list-E/hi_feeds_20241106_300x250_${
              elIndex * 1 + 1
            }', [300, 250], '${(adsID ? adsID : defaultID).replace(
          "-0",
          `-${index}`
        )}').addService(googletag.pubads());
            googletag.pubads().collapseEmptyDivs();
            googletag.enableServices();
          });
        googletag.cmd.push(function() { googletag.display('${(adsID
          ? adsID
          : defaultID
        ).replace("-0", `-${index}`)}'); }); console.log('广告代码执行了${
          adsID ? adsID : defaultID
        }');
      </script>
    </div>
  </div>`
      : [
          "id-feeds.mobicrazy.com",
          "en-in-feeds.obnews.co",
          "th-feeds.mobicrazy.com",
          "hi-feeds.mobicrazy.com",
        ].includes(window.location.hostname)
      ? `
<div class='plain-ads'>
      <div class='plain-ads-child' data-type="_mgwidget" data-widget-id="${
        adsID ? adsID : defaultID
      }"> 
      </div>
    <script class='script-copy'>(function(w,q){w[q]=w[q]||[];w[q].push(["_mgc.load"])})(window,"_mgq"); console.log('广告代码执行了${
      adsID ? adsID : defaultID
    }')</script>
</div>
`
      : `<div class='plain-ads'>
          <ins 
            class="adsbygoogle"
            style="display:inline-block;width:300px;height:250px"
            data-ad-client="ca-pub-7725717395742252"
            data-ad-slot="${adsID ? adsID : defaultID}">
          </ins>
          <script class='script-copy'>(adsbygoogle = window.adsbygoogle || []).push({});console.log('广告代码执行了${
            adsID ? adsID : defaultID
          }');</script>
        </div>`;

  var hostname = window.location.hostname;
  console.log(parent, hostname);
  if (!window.domains.includes(hostname)) {
    var doc = parser.parseFromString(frament, "text/html");
    var script = doc.querySelector("script");
    var newscript = document.createElement("script");
    newscript.text = script.textContent;
    var plainAds = doc.querySelector(".plain-ads");
    parent.insertBefore(plainAds, target);
    plainAds.appendChild(newscript);
    document.querySelectorAll(".script-copy").forEach((el) => {
      el.remove();
    });
  } else {
    var doc = parser.parseFromString(frament, "text/html");
    var script = doc.querySelector("script");
    var newscript = document.createElement("script");
    newscript.text = script.textContent;
    var plainAds = doc.querySelector(".plain-ads");
    var plainAdsChild = doc.querySelector(".plain-ads-child");
    plainAds.innerHTML = "";
    plainAds.appendChild(plainAdsChild);
    parent.insertBefore(plainAds, target);
    plainAdsChild.appendChild(newscript);
    document.querySelectorAll(".script-copy").forEach((el) => {
      el.remove();
    });
  }
}
function insertAfterAds(parent, target, elIndex) {
  var nextEl = target.nextElementSibling;
  if (nextEl && nextEl.classList.contains("plain-ads")) {
    return;
  }
  var adsID =
    window.plainAdsSlotID?.[window.location.hostname]?.[(elIndex / 3 - 1) % 4];
  insertAdsHTML(adsID, parent, nextEl, (elIndex / 3 - 1) % 4);
}
function insertBeforeAds(parent, target, elIndex) {
  var previousEl = target.previousElementSibling;
  if (previousEl && previousEl.classList.contains("plain-ads")) {
    return;
  }
  var adsID =
    window.plainAdsSlotID?.[window.location.hostname]?.[
      ((elIndex - 1) / 3 - 1) % 4
    ];
  insertAdsHTML(adsID, parent, target, ((elIndex - 1) / 3 - 1) % 4);
}
